﻿using System;
using EPiServer.Core;
using EPiServer.Framework;
using EPiServer.Web;
using log4net;
using TemplateFoundation.Reflection;

namespace TemplateFoundation.EventHandlers
{
    /// <summary>
    /// Initializes EPiServer Template Foundation to add things such as translations to the LanguageManager
    /// </summary>
    [InitializableModule]
    [ModuleDependency(typeof(InitializationModule))]
    public class TemplateFoundationInitializer : IInitializableModule
    {
        private static readonly ILog _logger = LogManager.GetLogger(typeof (TemplateFoundationInitializer));

        private static bool _initialized;

        #region IInitializableModule Members

        /// <summary>
        /// Initializes EPiServer Template Foundation
        /// </summary>
        /// <param name="context"></param>
        public void Initialize(EPiServer.Framework.Initialization.InitializationEngine context)
        {
            if (!_initialized)
            {
                _logger.Debug("[ETF] Initializing EPiServer Template Foundation...");

                // Load embedded language file for general translations
                try
                {
                    LanguageManager.Instance.LanguageDocument.Load(
                        EmbeddedResourceLoader
                        .LoadResource(
                            "Resources.Translations.TemplateFoundation.xml",
                            typeof(TemplateFoundationInitializer).Assembly));
                }
                catch
                {
                    _logger.Warn("[ETF] Unable to load language XML for EPiServer Template Foundation");
                }

                // Load embedded language file for PageTypeBase page type properties
                try
                {
                    LanguageManager.Instance.LanguageDocument.Load(
                        EmbeddedResourceLoader
                        .LoadResource(
                            "Resources.Translations.PageTypeBase.xml", 
                            typeof (TemplateFoundationInitializer).Assembly));
                }
                catch
                {
                    _logger.Warn("[ETF] Unable to load language XML for ETF PageTypeBase");
                }

                // Load embedded language file for tabs
                try
                {
                    LanguageManager.Instance.LanguageDocument.Load(
                        EmbeddedResourceLoader
                        .LoadResource(
                            "Resources.Translations.Tabs.xml",
                            typeof(TemplateFoundationInitializer).Assembly));
                }
                catch
                {
                    _logger.Warn("[ETF] Unable to load language XML for ETF tabs");
                }

                // Load embedded language file for start page
                try
                {
                    LanguageManager.Instance.LanguageDocument.Load(
                        EmbeddedResourceLoader
                        .LoadResource(
                            "Resources.Translations.StartPageBase.xml",
                            typeof(TemplateFoundationInitializer).Assembly));
                }
                catch
                {
                    _logger.Warn("[ETF] Unable to load language XML for ETF start page");
                }

                _initialized = true;
            }
        }

        /// <summary>
        /// Not implemented, no preload required
        /// </summary>
        /// <param name="parameters"></param>
        public void Preload(string[] parameters)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Not implemented, no cleanup required
        /// </summary>
        /// <param name="context"></param>
        public void Uninitialize(EPiServer.Framework.Initialization.InitializationEngine context)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
